import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

public class EntreeSortie {
	
	static Console console;
	static Logo logo;
	static String note;
	
	public HashMap<String,Integer> argFctPredefinie = new HashMap<String,Integer>();
	
	public HashMap<String,Method> methodFctPredefinie = new HashMap<String,Method>();
	
	public EntreeSortie(Logo logo) {
		EntreeSortie.console=logo.getConsol();
		EntreeSortie.logo=logo;
		note="";
				
		argFctPredefinie.put("ecris", 1);
		argFctPredefinie.put("montre", 1);
		argFctPredefinie.put("erreur", 1);
		argFctPredefinie.put("systeme", 1);
		argFctPredefinie.put("lireliste", 0);
		argFctPredefinie.put("liremot", 0);
		argFctPredefinie.put("liremot", 0);
		argFctPredefinie.put("noter", 1);
		
		Class c = EntreeSortie.class;
		Method[] tab = c.getMethods();
		
		Method  m;
		for (int i=0;i<tab.length;i++) {
			m = tab[i];
			if(m.getName().equals("ecris"))
				methodFctPredefinie.put("ecris", m);
			if(m.getName().equals("montre"))
				methodFctPredefinie.put("montre", m);
			if(m.getName().equals("erreur"))
				methodFctPredefinie.put("erreur", m);
			if(m.getName().equals("systeme"))
				methodFctPredefinie.put("systeme", m);
			if(m.getName().equals("lireliste"))
				methodFctPredefinie.put("lireliste", m);
			if(m.getName().equals("liremot"))
				methodFctPredefinie.put("liremot", m);
			if(m.getName().equals("noter"))
				methodFctPredefinie.put("noter", m);
		}    
	}
	
	//ecris [1 2 3] -> 1 2 3
	public  static Object ecris(ArrayList<Object> v,  HashMap<String, Object> penv) throws ParseException, Exception{		
		if(v.size()==1) {
			if(v.get(0) instanceof ArrayList) 
				{				
				note+=listToString((ArrayList)v.get(0));
				console.out(note);
				note="";
				}			    
			else
				{
				note+=v.get(0).toString();
				console.out(note);
				note="";
				}
			return true;
		}
		else 
			return false;
	}
	
	//montre [1 2 3] -> [1 2 3] 
	//!\\ PAS IMPLEMENTER
	public  static Object montre(ArrayList<Object> v,  HashMap<String, Object> penv) throws ParseException, Exception{		
		if(v.size()==1) {
			if(v.get(0) instanceof ArrayList) 
				console.out(listToString((ArrayList)v.get(0)));
			else
				console.out(v.get(0).toString());
			return true;
		}
		else 
			return false;
	}
	
	
	
	public  static Object erreur(ArrayList<Object> v,  HashMap<String, Object> penv) throws ParseException, Exception{		
		if(v.size()==1) {
			if(v.get(0) instanceof ArrayList) 
				console.err(listToString((ArrayList)v.get(0)));
			else
				console.err((String) v.get(0));
			return true;
		}
		else 
			return false;
	}
	
	public  static Object systeme(ArrayList<Object> v,  HashMap<String, Object> penv) throws ParseException, Exception{		
		if(v.size()==1) {
			if(v.get(0) instanceof ArrayList) 
				console.sys(listToString((ArrayList)v.get(0)));
			else
				console.sys((String) v.get(0));
			return true;
		}
		else 
			return false;
	}
	
	private static String listToString(ArrayList l){
		String list = "[ ";
		
		for(int i=0; i<l.size();i++){
			if(l.get(i) instanceof ArrayList)
				list += listToString((ArrayList)l.get(i))+" ";
			else
				list += l.get(i) +" ";
		}
		list += "]";
		return list;
	}

	public static Object lireliste(ArrayList<Object> v,  HashMap<String, Object> penv) {
		ArrayList AL = new ArrayList();
		String liste = logo.lireMessage("lireliste");
		int debut = 0;
		for(int fin=0; fin<liste.length();fin++)
		{
			if(liste.charAt(fin)==' ')
			{
				AL.add(liste.substring(debut,fin));			
				debut=fin+1;
			}
		}
		AL.add(liste.substring(debut,liste.length()));
		return AL;
	}
	
	public static Object liremot(ArrayList<Object> v,  HashMap<String, Object> penv) {
		String liste = logo.lireMessage("liremot");
		int fin=liste.indexOf(' ');
		if (fin<0) fin=liste.length();			
		return liste.substring(0,fin);
	}
	
	public static Object noter(ArrayList<Object> v,  HashMap<String, Object> penv) {
		note+=(String) v.get(0);
		System.out.println(note);
		return true;
	}
	

}
